<?php 
namespace common\libs;

class StringHelper{
	
	
	/**
	 * 随机6位验证码
	 * @param number $str
	 * @return string
	 */
	public static function  getRandomCode($length=6){
	
		$chars = "123456789abcdefghijklmnopkrstuvwxyz";
		$str = "";
		for($i = 0; $i < $length; $i++){
			$str .= $chars[mt_rand(0, strlen($chars) - 1)];
		}
		return $str;
	}
	
	public static function fomartTime($time){
		$time = time() - $time;
		$year =  intval(($time/86400)/365);
		$month =  intval(($time/86400)/30);
		$day = intval(($time/86400));
		$houre = intval(($time%86400)/3600);
		$minute = intval($time/60);
		if($year>0){
			return $year."年前";
		}elseif($month>0){
			return $month."月前";
		}elseif($day>0){
			return $day."天前";
		}elseif($houre>0){
			return $houre."小时前";
		}elseif($minute>1){
			return $minute."分钟前";
		}else{
			return "刚刚";
		}
	}
	
	static function PassStart($str,$start,$end=0,$dot="*",$charset="UTF-8"){
		$len = mb_strlen($str,$charset);
		if($start==0||$start>$len){
			$start = 1;
		}
		if($end!=0&&$end>$len){
			$end = $len-2;
		}
		$endStart = $len-$end;
		$top = mb_substr($str, 0,$start,$charset);
		$bottom = "";
		if($endStart>0){
			$bottom =  mb_substr($str, $endStart,$end,$charset);
		}
		$len = $len-mb_strlen($top,$charset);
		$len = $len-mb_strlen($bottom,$charset);
		$newStr = $top;
		for($i=0;$i<$len;$i++){
			$newStr.=$dot;
		}
		$newStr.=$bottom;
		return $newStr;
	}
	
	public static function xml_to_json($xmlString){
		$json = simplexml_load_string($xmlString, 'SimpleXMLElement', LIBXML_NOCDATA);
		$json = json_decode(json_encode($json),true);  
		return $json;
	}
	
	/**
	 * 获取最近一周的时间戳
	 * @param string $date 日期时间
	 * @return array
	 */
	public static function getWeekTimeScope($date = '')
	{
		if (empty($date)) {
			$date = date('Y-m-d H:i:s', time());
		}
		$timestamp = strtotime($date);

		return array(
			'lastTime' => $timestamp,
			'startTime' => strtotime('-7 day', $timestamp),
		);
	}

	/**
	 * 获取最近一月的时间戳
	 * @param string $date 日期时间
	 * @return array
	 */
	public static function getMonthTimeScope($date = '')
	{
		if (empty($date)) {
			$date = date('Y-m-d H:i:s', time());
		}
		$timestamp = strtotime($date);

		return array(
			'lastTime' => $timestamp,
			'startTime' => strtotime('-30 day', $timestamp),
		);
	}

	public static function get62($src, $length = 8)
	{
		$md5 = md5($src, true);

		$pos = 0;
		$res = "";
		while(strlen($res) < $length && ($bin = substr($md5, $pos, 4)) != ""){
			$uint =  sprintf("%u" , unpack("Nint", $bin)['int']);
			$res .= self::decto62($uint);
			$pos += 4;
		}
		return substr($res, 0, $length);
	}

	public static function decto62($src)
	{
		static $table = [];
		$table = array_merge(range(0, 9), range('A', "Z"), range('a', "z"));

		$arr62 = [];
		$div = $src;
		do{
			$mod = $div % 62;
			array_unshift($arr62, $table[$mod]);
			$div = intval($div / 62);
		}while($div != 0);

		return implode("", $arr62);
	}

	/**
	 * 获取当日时间范围
	 * @param string $date 日期时间
	 * @return array
	 */
	public static function getDayTimeScope($date = NULL)
	{
		if (empty($date)) {
			$date = date('Y-m-d', time());
		}
		$timestamp = strtotime($date);

		return array(
			'endTime' => strtotime('1 day', $timestamp),
			'startTime' => $timestamp,
		);
	}

	/**
	 * 求两个日期之间相差的天数
	 * @param string $day1
	 * @param string $day2
	 * @return number
	 */
	public static function getDiffDays($day1, $day2)
	{
		$second1 = strtotime($day1);
		$second2 = strtotime($day2);
	    
		if ($second1 < $second2) {
			$tmp = $second2;
			$second2 = $second1;
			$second1 = $tmp;
		}

	  	return ($second1 - $second2) / 86400;
	}

	/**
     * 获取steam图片地址
     * @param $appId
     * @param $marketHashName
     * @return string
     */
    public static function getSteamImageUrl($appId, $marketHashName)
    {
        $steamItemImgUrl = rtrim(getenv('STEAMITEM_IMG_URL'), '/');
        return $steamItemImgUrl . '/' . urlencode(base64_encode($appId . $marketHashName)) . '.png';
    }

    public static function formatLeftTime($leftTime){
		if ($leftTime <= 0) {
			return "0分钟";
		}

		$day = intval(($leftTime/86400));
		$houre = intval(($leftTime%86400)/3600);
		$minute = intval(($leftTime%86400%3600)/60);

		if ($day > 0) {
			return $day."天".$houre.'小时';
		} elseif ($houre > 0) {
			return $houre.'小时';
		} elseif ($minute > 0) {
			return $minute.'分钟';
		}
        return "0分钟";
	}
}